<?
class Controller {
    
    public $paypal;
    
    public function __construct(){
        $this->query = $_SERVER["SCRIPT_URL"];
        // Database
        $DB = new Database();
        $this->connections = $DB;
        
        // helpers
        $paypal = new PaypalHelper("test");
        $this->paypal = $paypal;
        
    }
    public function __exec(){
        // Route
        $router = new Router(false);
        $route = $router->getRoute();       
        
        if(isset($route->secured) && $route->secured){
            if(!isset($_SESSION["logged"])){
                throw new SecureException ("You must be logged to access to this page");
            }
        }
        $urlParams = $route->urlParams;	
        $controllerName = ucfirst($route->controller)."Controller";
        $action = $route->action."Action";
        
        $this->controller = $controllerName;
        $this->action = $action;
        
        $controller = controllers_path."/".ucFirst($controllerName).".php";
        if(!file_exists($controller)){
            throw new Exception ("Controller ".$controllerName." Not found");
        }else{
            include_once $controller;
        }
        if(!class_exists($controllerName)){
            throw new Exception ("Controller ".$controllerName." Does not exists");
        }
        if(!method_exists($controllerName,$action)){
            throw new Exception ("Method $action does not exists in $controllerName");
        }
        $controller = new $controllerName();
        $response = @call_user_func_array(array($controller, $action ), $urlParams);        
        echo $response;        
    }
    public static function displayErrorPage($e){
        $message = $e->getMessage();
        $details = "<pre>".print_r($e->getTrace(),true)."</pre>";
        include errorPages_path."/error.php";
    }
    public function render($viewName,$args = array()){
        if(!file_exists(views_path."/".$viewName.".php")){
            throw new Exception ("View $viewName does not exists");
        }
    
        foreach($args as $var=>$val) $$var = $val;
        ob_start();
        include views_path."/".$viewName.".php";
        $content = ob_get_clean();
        if(!empty($this->extendedContent)){
            $content = str_replace("{% Content %}",$content,$this->extendedContent);
        }else{
            $content = str_replace("{% Content %}","",$content);
        }
        return $content;
    }
    public function extend($name, $args = array()){
        foreach($args as $var=>$val) $$var = $val;
        ob_start();
        include views_path."/".$name.".php";
        $content = ob_get_clean();
        $this->extendedContent = $content;
    }
    public function inc($name,$args=array()){
        foreach($args as $var=>$val) $$var = $val;
        ob_start();
        include views_path."/".$name.".php";
        $content = ob_get_clean();
        return $content;
    }
    public function js($file){
        return "\t<script type=\"text/javascript\" src=\"".base_path."/public/".$file."\"></script>\n";
    }
	public function css($file){
        return "\t".'<link rel="stylesheet" href="'.base_path.'/public/'.$file.'" type="text/css" media="all" />'."\n";
    }
    public function printSelect($table,$name="",$id=""){
		  $html = "<select name='$name' id='$id'>";
				foreach($table as $var=>$val){
					 $html .= "<option value=\"$var\">$val </option>";
				}
		  $html .= "</select>";
		  return $html;
    }
}
?>